<script type="text/ecmascript-6">

  import Navbar from '../../components/base/navbar.vue';
  import {query} from '../../utils/util';
//  import { submitChutComments } from '../../datas/actions/chub';

  export default {
    components: {
      Navbar
//      vTextarea
    },
    data() {
      return {
        urlStatus: 0,
        url: '',
        content: '',
        minLength:0,
        maxLength:140
      }
    },
    methods: {
      _keyup(){
          if(this.content.length>this.maxLength){
              this.content=this.content.substr(0,this.maxLength);
          }
          let searchs=query(window.location.href);
        if(this.content){
          if(!searchs.edit){
            window.history.pushState({edit:1},'',this.url);
          }
        }else{
          if(searchs.edit){
            window.history.back();
          }
        }
      },
      exit(){
        if(!this.content) return;
        if(this.urlStatus) return;
//        const msg = confirm('确定退出编辑吗？');
        this.$Modal.info({title:'消息提示',body:"确定退出编辑吗？",onOk:this.close,onCancle:this.cancle})
      },
      post(){
        if(this.content){
            this.$store.dispatch("submitChutComments",{article_id:this.$route.params.id,comment:this.content})
//          this.submitChutComments(this.$route.params.id, this.content);
        }else{
//          this.$Alert('评论内容不能为空！');
          this.$Modal.info({title:'消息提示',body:'评论内容不能为空'});
        }
      },
      close(){
//        console.log(1);
//        this.urlStatus = 1;
        this.content="";
        window.history.back();
      },
      cancle(){
//          console.log(1);
        window.history.pushState({edit:1},'', this.url);
      },
      CompleteSubmitComment(){
        window.removeEventListener('popstate', this.exit);
        this.$Message.show({text:"评论成功"});
        this.content="";
        window.history.go(-2);
      }
    },
    mounted() {

      const url=this.$route.path+"?edit=1";
      if(this.$router.mode === 'hash') this.url="#"+url;
      const searchs = query(window.location.href);
      console.log(searchs);
      if(searchs.edit){
        window.history.back();
      }

      window.addEventListener('popstate', this.exit, false);
    },
    beforeDestroy() {
      window.removeEventListener('popstate', this.exit);
    },
    created(){
        EventBus.$on('complete-submit-comment',this.CompleteSubmitComment);
    }
//    activated(){
//        EventBus.$on('complete-submit-comment',this.CompleteSubmitComment);
//        const url=this.$route.path+"?edit=1";
//        if(this.$router.mode === 'hash') this.url="#"+url;
//        const searchs = query(window.location.href);
//        console.log(searchs);
//        if(searchs.edit){
//          window.history.back();
//        }
//
//        window.addEventListener('popstate', this.exit, false);
//    }
//   ,
//    events: {
//      CompleteSubmitComment(){
//        window.removeEventListener('popstate', this.exit);
//        this.$Success('评论成功');
//        window.history.go(-2);
//      }
//    },
//    vuex: {
//      actions: {
//        submitChutComments
//      }
//    }
  }

</script>
